package com.hanbang.chu_xing_gao_zhao.view_find;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.base.MainActivity;
import com.hanbang.chu_xing_gao_zhao.myview.NoScrollViewPager;
import com.hanbang.cxgz_2.R;

import java.util.ArrayList;

/**
 * 秘籍展示
 *
 * @author yang
 */
public class FindMainFragmentActivity extends MainActivity implements OnClickListener {
    private LinearLayout find;
    private NoScrollViewPager viewPager;
    private FragmentActivityAdapter adapter;
    private ArrayList<Fragment> fragments;
    private ArrayList<TextView> tvList = new ArrayList<TextView>();
    private TextView t1, t2, t3, t4;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_main_fragment_activity);

        initView();
        setOnClickListener();
        setUpViewsAndAddLinstener(find);


    }


    /**
     * 初始化view
     */
    private void initView() {
        find = (LinearLayout) findViewById(R.id.bar_botton_bar_find);
        setTitleMore(View.GONE, View.GONE, "找大厨", "找掌柜", "找团伙", "附近");
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));

        t1 = (TextView) findViewById(R.id.tv_bar_top_conver_classify_title1);
        t2 = (TextView) findViewById(R.id.tv_bar_top_conver_classify_title2);
        t3 = (TextView) findViewById(R.id.tv_bar_top_conver_classify_title3);
        t4 = (TextView) findViewById(R.id.tv_bar_top_conver_classify_title4);

        tvList.add(t1);
        tvList.add(t2);
        tvList.add(t3);
        tvList.add(t4);

        fragments = new ArrayList<Fragment>();
        viewPager = (NoScrollViewPager) findViewById(R.id.find_main_fragment_activity_viewPage);
        viewPager.setNoScroll(true);

        fragments.add(new FindChefFragment());
        fragments.add(new FindManageFragment());
        fragments.add(new FindGangFragment());
        fragments.add(new FindNearbyMapFragment());

        adapter = new FragmentActivityAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        setColor(0);

        Intent intent = getIntent();
        int page = intent.getIntExtra("type", 0);
        setColor(page);

    }


    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        t1.setOnClickListener(this);
        t2.setOnClickListener(this);
        t3.setOnClickListener(this);
        t4.setOnClickListener(this);


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    //大厨课堂
                    case 0:
                        setColor(0);
                        viewPager.setCurrentItem(0);
                        break;
                    //掌柜课堂
                    case 1:
                        setColor(1);
                        viewPager.setCurrentItem(1);
                        break;
                    //学习资料
                    case 2:
                        setColor(2);
                        viewPager.setCurrentItem(2);
                        break;
                    //附近
                    case 3:
                        setColor(3);
                        viewPager.setCurrentItem(3);
                        break;

                    default:

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        Intent intent = new Intent();

        switch (v.getId()) {
            //大厨课堂
            case R.id.tv_bar_top_conver_classify_title1:

                setColor(0);
                viewPager.setCurrentItem(0);
                break;
            //掌柜课堂
            case R.id.tv_bar_top_conver_classify_title2:

                setColor(1);
                viewPager.setCurrentItem(1);
                break;
            //找团伙
            case R.id.tv_bar_top_conver_classify_title3:

                setColor(2);
                viewPager.setCurrentItem(2);
                break;
            //附近
            case R.id.tv_bar_top_conver_classify_title4:
                setColor(3);
                viewPager.setCurrentItem(3);
                break;

            default:

                break;
        }


    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    /**
     * intent 的回调方法
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


    }

    /**
     * viewPage 适配器
     */
    public class FragmentActivityAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> fragments;
        FragmentManager fm;

        public FragmentActivityAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            if (fragments == null) {

                this.fragments = list;
                this.fm = fm;

            } else {
                this.fragments.clear();
                fragments = new ArrayList<Fragment>();
            }
        }


        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }

        @Override
        public int getCount() {

            return fragments.size();
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return super.isViewFromObject(view, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);

        }
    }

    /**
     * 更改标题背景颜色
     *
     * @param
     */
    public void setColor(int id) {
        for (int i = 0; i < tvList.size(); i++) {
            if (id == i) {
                tvList.get(i).setBackgroundResource(R.color.white);
            } else {
                tvList.get(i).setBackgroundResource(R.color.yellow);
            }
        }

    }
}


